//
//  IAPNative.m
//  rn_jkzx
//
//  Created by 华生升 on 2018/5/24.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "IAPNative.h"
#import "IAPShare.h"
#import "AddressViewController.h"
@implementation IAPNative


RCT_EXPORT_MODULE();
// RN跳转原生界面
// RNOpenOneVC指的就是跳转的方法，下面会用到
RCT_EXPORT_METHOD(RNOpenIAP:(NSString *)msg){
  NSLog(@"RN传入原生界面的数据为:%@",msg);
  dispatch_async(dispatch_get_main_queue(), ^{
    [self iapBuy:msg];
  });
  
}
-(void)iapBuy:(NSString *)proudId{
//  if(![IAPShare sharedHelper].iap) {
    NSSet* dataSet = [[NSSet alloc] initWithObjects:proudId, nil];
    
    [IAPShare sharedHelper].iap = [[IAPHelper alloc] initWithProductIdentifiers:dataSet];
//  }
  
  [IAPShare sharedHelper].iap.production = NO;
  
  [[IAPShare sharedHelper].iap requestProductsWithCompletion:^(SKProductsRequest* request,SKProductsResponse* response)
   {
     if(response > 0 ) {
       SKProduct* product =[[IAPShare sharedHelper].iap.products objectAtIndex:0];
       
       NSLog(@"Price: %@",[[IAPShare sharedHelper].iap getLocalePrice:product]);
       NSLog(@"Title: %@",product.localizedTitle);
       
       [[IAPShare sharedHelper].iap buyProduct:product
                                  onCompletion:^(SKPaymentTransaction* trans){
                                    
                                    if(trans.error)
                                    {
                                      NSLog(@"Fail %@",[trans.error localizedDescription]);
                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"iap-emitted" object:@{@"msg":[trans.error localizedDescription],@"code":@"400"}];
                                    }
                                    else if(trans.transactionState == SKPaymentTransactionStatePurchased) {
                                      
                                      [[IAPShare sharedHelper].iap checkReceipt:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]] AndSharedSecret:@"your sharesecret" onCompletion:^(NSString *response, NSError *error) {
                                        
                                        //Convert JSON String to NSDictionary
                                        NSDictionary* rec = [IAPShare toJSON:response];
                                        
                                        if([rec[@"status"] integerValue]==0)
                                        {
                                          
                                          [[IAPShare sharedHelper].iap provideContentWithTransaction:trans];
                                          NSLog(@"SUCCESS %@",response);
                                          NSLog(@"Pruchases %@",[IAPShare sharedHelper].iap.purchasedProducts);
                                          [[NSNotificationCenter defaultCenter] postNotificationName:@"iap-emitted" object:@{@"msg":@"支付成功",@"code":@"200"}];
                                        }
                                        else {
                                          NSLog(@"Fail");
                                          [[NSNotificationCenter defaultCenter] postNotificationName:@"iap-emitted" object:@{@"msg":@"支付失败",@"code":@"400"}];
                                        }
                                      }];
                                    }
                                    else if(trans.transactionState == SKPaymentTransactionStateFailed) {
                                      NSLog(@"Fail");
                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"iap-emitted" object:@{@"msg":@"支付失败",@"code":@"400"}];
                                    }
                                  }];//end of buy product
     }
   }];
}
- (NSArray<NSString *> *)supportedEvents {
  return @[@"IAPNot"]; //这里返回的将是你要发送的消息名的数组。
}
- (void)startObserving
{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(emitEventInternal:)
                                               name:@"iap-emitted"
                                             object:nil];
}
- (void)stopObserving
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)emitEventInternal:(NSNotification *)notification
{
  [self sendEventWithName:@"IAPNot"
                     body:notification.object];
}
@end
